﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Provider;
using Android.Widget;
using Java.Nio;
using Buffer = Java.Nio.Buffer;
using Java.IO;

namespace GBU.Utility
{
    public static class BitmapHelper
    {
        /// <summary>
        /// This method will recyle the memory help by a bitmap in an ImageView
        /// </summary>
        /// <param name="imageView">Image view.</param>
        public static void RecycleBitmap(this ImageView imageView)
        {
            if (imageView == null)
            {
                return;
            }

            Drawable toRecycle = imageView.Drawable;
            if (toRecycle != null)
            {
                ((BitmapDrawable)toRecycle).Bitmap.Recycle();
            }
        }

        /// <summary>
        /// 将系统相对Uri转换为图片文件的绝对路径
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="contentResolver">[一般情况下请用this.contentResolver]</param>
        /// <returns></returns>
        public static string ToImageFilePath(this Android.Net.Uri uri, ContentResolver contentResolver)
        {
            var filePath = string.Empty;
            string[] projects = { MediaStore.Images.ImageColumns.Data };
            
            var cusror = contentResolver.Query(uri, projects, null, null, null);
            cusror.MoveToFirst();

            var indexc = cusror.GetColumnIndex(projects[0]);
            filePath = cusror.GetString(indexc);
            cusror.Close();

            return filePath;
        }

        public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
        {
            // First we get the the dimensions of the file on disk
            BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
            BitmapFactory.DecodeFile(fileName, options);

            // Next we calculate the ratio that we need to resize the image by
            // in order to fit the requested dimensions.
            int outHeight = options.OutHeight;
            int outWidth = options.OutWidth;
            int inSampleSize = 1;

            if (outHeight > height || outWidth > width)
            {
                inSampleSize = outWidth > outHeight
                                   ? outHeight / height
                                   : outWidth / width;
            }

            // Now we will load the image and have BitmapFactory resize it for us.
            options.InSampleSize = inSampleSize;
            options.InJustDecodeBounds = false;
            Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);
            return resizedBitmap;
        }

        public static byte[] LoadAndResizeBitmapBuffer(this string fileName, int width, int height)
        {
            byte[] buffer;
            using (var ms = new MemoryStream())
            {
                var img = fileName.LoadAndResizeBitmap(width, height);
                img.Compress(Bitmap.CompressFormat.Jpeg, 80, ms);
                img.Recycle();
                buffer=new byte[ms.Length];
                ms.Read(buffer, 0, buffer.Length);
            }

            return buffer;
        }
    }
}
